import { useReqQuery } from '@sker/runtime';
import { useTransaction } from '@sker/core';
import svgCaptcha from 'svg-captcha';
import { CommonCaptcha } from '@sker/entities'
import { z } from "zod";
import { md5 } from '@sker/core'
const input = z.object({
    width: z.string(),
    height: z.string(),
})
export default async () => {
    const query = input.parse(useReqQuery())
    const code = svgCaptcha.create({
        size: 4,
        noise: 0,
        color: true,
        background: '#fff',
        width: parseInt(query.width),
        height: parseInt(query.height),
        ignoreChars: '0ilol'
    })
    // 防止被爆破
    const hashkey = md5(`${code.text}${Date.now()}`).toLowerCase();
    useTransaction(async (m) => {
        await m.insert(CommonCaptcha, {
            challenge: code.text,
            response: code.text,
            hashkey: hashkey,
            expire_date: new Date(Date.now() + 1000 * 60 * 60)
        })
    })
    return {
        hashkey: hashkey,
        data: code.data,
        success: true,
        message: 'ok'
    }
}
